package com.hex.ds.hdtp.core.web.data.contrast.controller;

import com.alibaba.fastjson.JSON;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.request.CrossDbContrastListRequestDto;
import com.hex.ds.hdtp.core.app.data.contrast.dto.request.CrossDbContrastTaskRequestDto;
import com.hex.ds.hdtp.core.app.data.contrast.service.ICrossDbContrastListService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.data.contrast.converter.ICrossDbContrastListVoConverter;
import com.hex.ds.hdtp.core.web.data.contrast.vo.CrossDbContrastListVo;
import com.hex.ds.hdtp.core.web.data.contrast.vo.query.CrossDbContrastListQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

/**
 * @Package: com.hex.ds.hdtp.core.web.data.contrast.controller
 * @ClassName CrossDbContrastListController
 * @Description: 跨库比对清单-WEB接口
 * @Author: wz.li
 * @Date 2023/7/24 19:35
 * @Version v2.0.2
 **/
@Slf4j
@RestController
@Api(tags = "05-跨库比对：清单")
@RequestMapping("/api/cross/db/contrast/list")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CrossDbContrastListController {

    private final ICrossDbContrastListService crossDbContrastListService;

    private final ICrossDbContrastListVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /*
     * @Method: queryListByPage <br>
     * @Param: [crossDbContrastListQueryVo, pageVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库比对清单-分页查询<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 19:37 <br>
     * @Version： V2.0.2<br>
     */
    @GetMapping
    @ApiOperation("跨库比对清单-分页查询")
    public ResponseEntity queryListByPage(CrossDbContrastListQueryVo crossDbContrastListQueryVo, PageVo pageVo) {
        log.debug("进入到【CrossDbContrastListController.queryListByPage】方法,参数：{}" + crossDbContrastListQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        CrossDbContrastListRequestDto requestDto = converter.toRequestDtoByQuery(crossDbContrastListQueryVo);
        return ResponseEntity.ok(crossDbContrastListService.queryListByPage(requestDto, pageDto));
    }

    /**
     * @param crossDbContrastListQueryVo
     * @param pageVo
     * @Method queryListResultByPage
     * @Param
     * @Return org.springframework.http.ResponseEntity
     * @Description 跨库比对清单的结果-分页查询
     * @Author Wang zhihao
     * @Date 2023/8/4 11:44
     * @Version V1.0
     */
    @GetMapping("/result")
    @ApiOperation("跨库比对清单的结果-分页查询")
    public ResponseEntity queryListResultByPage(CrossDbContrastListQueryVo crossDbContrastListQueryVo, PageVo pageVo) {
        log.debug("进入到【CrossDbContrastListController.queryListByPage】方法,参数：{}" + crossDbContrastListQueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        CrossDbContrastListRequestDto requestDto = converter.toRequestDtoByQuery(crossDbContrastListQueryVo);
        return ResponseEntity.ok(crossDbContrastListService.queryListResultByPage(requestDto, pageDto));
    }

    /*
     * @Method: add <br>
     * @Param: [crossDbContrastListVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库比对清单-新增<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 19:38 <br>
     * @Version： V2.0.2<br>
     */
    @Log("跨库比对清单-新增")
    @PostMapping
    @ApiOperation("跨库比对清单-新增")
    public ResponseEntity add(@RequestBody CrossDbContrastListVo crossDbContrastListVo) {
        log.info("进入到【CrossDbContrastListController.add】方法,参数：{}", crossDbContrastListVo.toString());
        return ResponseEntity.ok(crossDbContrastListService.addWithBackId(converter.toRequestDto(crossDbContrastListVo)));
    }

    /*
     * @Method: update <br>
     * @Param: [crossDbContrastListVo] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库比对清单-更新<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 19:38 <br>
     * @Version： V2.0.2<br>
     */
    @Log("跨库比对清单-更新")
    @PutMapping
    @ApiOperation("跨库比对清单-更新")
    public ResponseEntity update(@RequestBody CrossDbContrastListVo crossDbContrastListVo) {
        log.info("进入到【CrossDbContrastListController.update】方法,参数：{}", crossDbContrastListVo.toString());
        crossDbContrastListService.modifyById(converter.toRequestDto(crossDbContrastListVo));
        return ResponseEntity.ok("更新成功");
    }

    /*
     * @Method: delete <br>
     * @Param: [pkIds] <br>
     * @Return: org.springframework.http.ResponseEntity <br>
     * @Description：跨库比对清单-批量删除<br>
     * @Author： wz.li<br>
     * @Date： 2023/7/24 19:38 <br>
     * @Version： V2.0.2<br>
     */
    @Log("跨库比对清单-批量删除")
    @DeleteMapping(value = {"/{pkIds}"})
    @ApiOperation("跨库比对清单-批量删除")
    public ResponseEntity delete(@PathVariable("pkIds") @NotNull List<String> pkIds) {
        log.info("进入【CrossDbContrastListController.delete】方法,参数：" + pkIds);
        crossDbContrastListService.batchRemoveById(pkIds);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * @Method downloadJobDef
     * @Param
     * @Param crossDbContrastListQueryVo
     * @Param pageVo
     * @Return org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description 日志导出
     * @Author xin.zhao
     * @Date 2023/11/8 11:17
     * @Version V1.0
     */
    @Log("导出对比结果")
    @ApiOperation("导出对比结果")
    @GetMapping(value = "/result/download")
    public ResponseEntity<Object> downloadJobDef(CrossDbContrastListQueryVo crossDbContrastListQueryVo, PageVo pageVo) throws IOException {
        log.info("导出周期作业任务【PeriodJobDefController.downloadJobDef】");
        String filePath = crossDbContrastListService.exportExcel(converter.toRequestDtoByQuery(crossDbContrastListQueryVo), pageVoAndPageDtoConverter.toPageDto(pageVo));
        byte[] bytes = Files.readAllBytes(new File(filePath).toPath());
        ByteArrayResource bar = new ByteArrayResource(bytes);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=contrastResult.zip")
                .body(bar);
    }

    @Log("导出对比清单")
    @ApiOperation("导出对比清单")
    @GetMapping(value = "/exportContrastList")
    public ResponseEntity<Object> exportContrastList(@RequestParam List<String> pkIds) {
        log.info("进入到【CrossDbContrastListController.exportContrastList】方法,参数：{}", pkIds);
        ByteArrayResource bar = crossDbContrastListService.exportContrastList(pkIds);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=exportContrastList.zip")
                .body(bar);
    }

    @Log("导入对比清单")
    @ApiOperation("导入对比清单")
    @PostMapping(value = "/importContrastList")
    public ResponseEntity<Object> importContrastList(@RequestBody MultipartFile file, @RequestParam String contrastTask) {
        log.info("进入到【CrossDbContrastListController.importContrastList】方法,参数：{},{}", file, contrastTask);
        Map<String, Object> resultMap = crossDbContrastListService.importContrastList(file, JSON.parseObject(contrastTask, CrossDbContrastTaskRequestDto.class));
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }
}
